namespace SmartSoft.SystemForms
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Properties;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormUserEditor : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnSave;
        private CBoxControl cBoxGrpCode;
        private ChkControl chkNotDelete;
        private IContainer components;
        private bool ib_EditMyself;
        private bool ib_NewChecked;
        private string is_AddTitleText = "Thêm người sử dụng";
        private string is_EditTitleText = "Sửa người sử dụng";
        private LabelControl lblFullName;
        private LabelControl lblGrpCode;
        private LabelControl lblNotDelete;
        private LabelControl lblUser_PWD;
        private LabelControl lblUserName;
        private DataRow oCurrentRow;
        private FormUserLister oLocalFormLister;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStrip Toolbars;
        private TextControl txtFullName;
        private TextControl txtUser_PWD;
        private CBoxControl cBoxDvcs;
        private LabelControl labelControl1;
        public TextControl txtUserName;

        public FormUserEditor()
        {
            this.InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                this.NewChecked = true;
                this.OnReset();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                base.Close();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private string GetTitleMessage()
        {
            if (!this.NewChecked)
            {
                return this.EditTitleText;
            }
            return this.AddTitleText;
        }

        private void InitializeComponent()
        {
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.lblFullName = new SmartSoft.Controls.LabelControl();
            this.lblUserName = new SmartSoft.Controls.LabelControl();
            this.txtFullName = new SmartSoft.Controls.TextControl();
            this.txtUserName = new SmartSoft.Controls.TextControl();
            this.txtUser_PWD = new SmartSoft.Controls.TextControl();
            this.lblUser_PWD = new SmartSoft.Controls.LabelControl();
            this.cBoxGrpCode = new SmartSoft.Controls.CBoxControl();
            this.lblGrpCode = new SmartSoft.Controls.LabelControl();
            this.chkNotDelete = new SmartSoft.Controls.ChkControl();
            this.lblNotDelete = new SmartSoft.Controls.LabelControl();
            this.cBoxDvcs = new SmartSoft.Controls.CBoxControl();
            this.labelControl1 = new SmartSoft.Controls.LabelControl();
            this.Toolbars.SuspendLayout();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnAdd,
            this.tbrSep01,
            this.btnSave,
            this.tbrSep02,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(525, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::SmartSoft.Properties.Resources.BALANCE1;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm mới (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSave
            // 
            this.btnSave.Image = global::SmartSoft.Properties.Resources.SAVE1;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(47, 22);
            this.btnSave.Text = "Lưu";
            this.btnSave.ToolTipText = "Ghi lại và Thoát (Ctrl+Enter)";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClose
            // 
            this.btnClose.Image = global::SmartSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // lblFullName
            // 
            this.lblFullName.AutoSize = true;
            this.lblFullName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblFullName.Location = new System.Drawing.Point(12, 53);
            this.lblFullName.Name = "lblFullName";
            this.lblFullName.Size = new System.Drawing.Size(61, 14);
            this.lblFullName.TabIndex = 7;
            this.lblFullName.Text = "Tên đầy đủ";
            // 
            // lblUserName
            // 
            this.lblUserName.AutoSize = true;
            this.lblUserName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblUserName.Location = new System.Drawing.Point(12, 31);
            this.lblUserName.Name = "lblUserName";
            this.lblUserName.Size = new System.Drawing.Size(68, 14);
            this.lblUserName.TabIndex = 8;
            this.lblUserName.Text = "Tên truy cập";
            // 
            // txtFullName
            // 
            this.txtFullName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtFullName.CheckTextSearched = false;
            this.txtFullName.DisabledForeColor = System.Drawing.Color.Empty;
            this.txtFullName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtFullName.Location = new System.Drawing.Point(123, 50);
            this.txtFullName.MaxLength = 128;
            this.txtFullName.Name = "txtFullName";
            this.txtFullName.OldValue = null;
            this.txtFullName.Size = new System.Drawing.Size(385, 20);
            this.txtFullName.TabIndex = 1;
            this.txtFullName.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemDescription;
            this.txtFullName.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.All;
            this.txtFullName.Value = global::SmartSoft.ReportForms.FormVTH02BViewer.txtTen_Nh_Vt_Value;
            // 
            // txtUserName
            // 
            this.txtUserName.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.txtUserName.CheckTextSearched = false;
            this.txtUserName.DisabledForeColor = System.Drawing.Color.Empty;
            this.txtUserName.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtUserName.Location = new System.Drawing.Point(123, 28);
            this.txtUserName.MaxLength = 32;
            this.txtUserName.Name = "txtUserName";
            this.txtUserName.OldValue = null;
            this.txtUserName.Size = new System.Drawing.Size(385, 20);
            this.txtUserName.TabIndex = 0;
            this.txtUserName.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemCode;
            this.txtUserName.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.All;
            this.txtUserName.Value = global::SmartSoft.ReportForms.FormVTH02BViewer.txtTen_Nh_Vt_Value;
            // 
            // txtUser_PWD
            // 
            this.txtUser_PWD.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtUser_PWD.CheckTextSearched = false;
            this.txtUser_PWD.DisabledForeColor = System.Drawing.Color.Empty;
            this.txtUser_PWD.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtUser_PWD.Location = new System.Drawing.Point(123, 72);
            this.txtUser_PWD.MaxLength = 128;
            this.txtUser_PWD.Name = "txtUser_PWD";
            this.txtUser_PWD.OldValue = null;
            this.txtUser_PWD.Size = new System.Drawing.Size(385, 20);
            this.txtUser_PWD.TabIndex = 2;
            this.txtUser_PWD.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemDescription;
            this.txtUser_PWD.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.None;
            this.txtUser_PWD.UseSystemPasswordChar = true;
            this.txtUser_PWD.Value = global::SmartSoft.ReportForms.FormVTH02BViewer.txtTen_Nh_Vt_Value;
            // 
            // lblUser_PWD
            // 
            this.lblUser_PWD.AutoSize = true;
            this.lblUser_PWD.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblUser_PWD.Location = new System.Drawing.Point(12, 75);
            this.lblUser_PWD.Name = "lblUser_PWD";
            this.lblUser_PWD.Size = new System.Drawing.Size(50, 14);
            this.lblUser_PWD.TabIndex = 7;
            this.lblUser_PWD.Text = "Mật khẩu";
            // 
            // cBoxGrpCode
            // 
            this.cBoxGrpCode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cBoxGrpCode.Font = new System.Drawing.Font("Arial", 8.25F);
            this.cBoxGrpCode.FormattingEnabled = true;
            this.cBoxGrpCode.Location = new System.Drawing.Point(123, 120);
            this.cBoxGrpCode.Name = "cBoxGrpCode";
            this.cBoxGrpCode.Size = new System.Drawing.Size(385, 22);
            this.cBoxGrpCode.TabIndex = 3;
            // 
            // lblGrpCode
            // 
            this.lblGrpCode.AutoSize = true;
            this.lblGrpCode.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblGrpCode.Location = new System.Drawing.Point(12, 123);
            this.lblGrpCode.Name = "lblGrpCode";
            this.lblGrpCode.Size = new System.Drawing.Size(66, 14);
            this.lblGrpCode.TabIndex = 7;
            this.lblGrpCode.Text = "Thuộc nhóm";
            // 
            // chkNotDelete
            // 
            this.chkNotDelete.AutoSize = true;
            this.chkNotDelete.Enabled = false;
            this.chkNotDelete.Font = new System.Drawing.Font("Arial", 8.25F);
            this.chkNotDelete.Location = new System.Drawing.Point(123, 148);
            this.chkNotDelete.Name = "chkNotDelete";
            this.chkNotDelete.Size = new System.Drawing.Size(15, 14);
            this.chkNotDelete.TabIndex = 4;
            // 
            // lblNotDelete
            // 
            this.lblNotDelete.AutoSize = true;
            this.lblNotDelete.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblNotDelete.Location = new System.Drawing.Point(12, 149);
            this.lblNotDelete.Name = "lblNotDelete";
            this.lblNotDelete.Size = new System.Drawing.Size(63, 14);
            this.lblNotDelete.TabIndex = 7;
            this.lblNotDelete.Text = "Toàn quyền";
            // 
            // cBoxDvcs
            // 
            this.cBoxDvcs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cBoxDvcs.Font = new System.Drawing.Font("Arial", 8.25F);
            this.cBoxDvcs.FormattingEnabled = true;
            this.cBoxDvcs.Location = new System.Drawing.Point(123, 95);
            this.cBoxDvcs.Name = "cBoxDvcs";
            this.cBoxDvcs.Size = new System.Drawing.Size(385, 22);
            this.cBoxDvcs.TabIndex = 9;
            // 
            // labelControl1
            // 
            this.labelControl1.AutoSize = true;
            this.labelControl1.Font = new System.Drawing.Font("Arial", 8.25F);
            this.labelControl1.Location = new System.Drawing.Point(12, 98);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new System.Drawing.Size(71, 14);
            this.labelControl1.TabIndex = 10;
            this.labelControl1.Text = "Đơn vị cơ sở";
            // 
            // FormUserEditor
            // 
            this.ClientSize = new System.Drawing.Size(525, 174);
            this.Controls.Add(this.cBoxDvcs);
            this.Controls.Add(this.labelControl1);
            this.Controls.Add(this.chkNotDelete);
            this.Controls.Add(this.cBoxGrpCode);
            this.Controls.Add(this.lblNotDelete);
            this.Controls.Add(this.lblGrpCode);
            this.Controls.Add(this.lblUser_PWD);
            this.Controls.Add(this.lblFullName);
            this.Controls.Add(this.txtUser_PWD);
            this.Controls.Add(this.lblUserName);
            this.Controls.Add(this.txtFullName);
            this.Controls.Add(this.txtUserName);
            this.Controls.Add(this.Toolbars);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormUserEditor";
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected virtual void OnCancel()
        {
        }

        protected virtual void OnDelete()
        {
        }

        protected virtual void OnEdit()
        {
        }

        protected virtual void OnReset()
        {
            if (this.NewChecked)
            {
                this.txtUserName.Text = string.Empty;
                this.txtFullName.Text = string.Empty;
                this.txtUser_PWD.Text = string.Empty;
                this.chkNotDelete.Checked = false;
                this.cBoxGrpCode.SelectedIndex = 0;
            }
        }

        public virtual bool OnSave()
        {
            DataRow localCurrentRow;
            if (string.IsNullOrEmpty(this.txtUserName.Text))
            {
                MessageBox.Show("Chưa nhập tên truy cập!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (string.IsNullOrEmpty(this.txtFullName.Text))
            {
                MessageBox.Show("Chưa nhập tên đầy đủ!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.NewChecked)
            {
                localCurrentRow = this.LocalTable.NewRow();
            }
            else
            {
                localCurrentRow = this.LocalCurrentRow;
            }
            UserDataAccess access = new UserDataAccess();
            string text = this.txtUser_PWD.Text;
            text = access.Base64Encode(text.Equals(string.Empty) ? "SSSoft" : text);
            localCurrentRow["UserName"] = this.txtUserName.Text;
            localCurrentRow["FullName"] = this.txtFullName.Text;
            localCurrentRow["User_PWD"] = text;
            localCurrentRow["GrpCode"] = this.cBoxGrpCode.SelectedValue;
            localCurrentRow["GrpName"] = this.cBoxGrpCode.Text;
            localCurrentRow["NotDelete"] = this.chkNotDelete.Checked;
            localCurrentRow["Ma_Dvcs"] = this.cBoxDvcs.SelectedValue;
            if (this.ShowMessage(localCurrentRow, access.SaveRow(this.NewChecked, localCurrentRow)))
            {
                return false;
            }
            if (this.NewChecked)
            {
                this.LocalTable.Rows.Add(localCurrentRow);
            }
            if (this.ib_EditMyself)
            {
                AppInstance.UserName = this.txtUserName.Text;
                AppInstance.Password = this.txtUser_PWD.Text;
                AppInstance.FullName = this.txtFullName.Text;
            }
            this.LocalCurrentRow = localCurrentRow;
            this.LocalTable.AcceptChanges();
            this.txtUserName.Focus();
            if (!this.OrderFieldNames.Equals(string.Empty))
            {
                ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
            }
            this.LocalFormLister.SetPosition(this.LocalFieldName, this.LocalCurrentRow[this.LocalFieldName]);
            return true;
        }

        public virtual void OnShow()
        {
            DataTable table;            
            GroupDataAccess access = new GroupDataAccess();
            UserDataAccess access2 = new UserDataAccess();
            if (!this.NewChecked && this.LocalCurrentRow["UserName"].ToString().Equals(AppInstance.UserName))
            {
                this.ib_EditMyself = true;
                table = access.GetTable(this.LocalCurrentRow["GrpCode"].ToString(), true, 0);
            }
            else
            {
                table = access.GetTable(this.LocalCurrentRow["GrpCode"].ToString(), false, AppInstance.UserType);
            }
            this.cBoxGrpCode.DataSource = table;
            this.cBoxGrpCode.DisplayMember = "GrpName";
            this.cBoxGrpCode.SelectedIndex = 0;
            this.cBoxGrpCode.ValueMember = "GrpCode";
            string str = access2.Base64Decode(this.LocalCurrentRow["User_PWD"].ToString());
            this.txtUserName.Text = this.LocalCurrentRow["UserName"].ToString();
            this.txtFullName.Text = this.LocalCurrentRow["FullName"].ToString();
            this.txtUser_PWD.Text = str.Equals("SSSoft") ? "" : str;
            this.cBoxGrpCode.SelectedValue = this.LocalCurrentRow["GrpCode"];
            this.chkNotDelete.Checked = (bool) this.LocalCurrentRow["NotDelete"];

            CompanyDataAccess dvcsAccess = new CompanyDataAccess();
            DataTable dvcsTable = dvcsAccess.GetCompanies();
            this.cBoxDvcs.DataSource = dvcsTable;
            this.cBoxDvcs.DisplayMember = "Ten_Dvcs";
            this.cBoxDvcs.SelectedIndex = 0;
            this.cBoxDvcs.ValueMember = "Ma_Dvcs";
            this.cBoxDvcs.SelectedValue = this.LocalCurrentRow["Ma_Dvcs"];
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F2:
                    if (this.OnSave() && this.btnAdd.Enabled)
                    {
                        this.NewChecked = true;
                        this.OnReset();
                    }
                    return true;

                case Keys.F3:
                    if (this.btnSave.Enabled)
                    {
                        this.OnSave();
                    }
                    return true;

                case (Keys.Control | Keys.Return):
                    if (((base.ActiveControl is IDataGridViewEditingControl) && (base.ActiveControl.Parent != null)) && ((base.ActiveControl.Parent.Parent != null) && (base.ActiveControl.Parent.Parent is DataGridView)))
                    {
                        (base.ActiveControl.Parent.Parent as DataGridView).EndEdit();
                    }
                    if (this.btnSave.Enabled && this.OnSave())
                    {
                        base.Close();
                    }
                    return true;

                case Keys.Escape:
                    if (this.btnClose.Enabled)
                    {
                        base.Close();
                    }
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public bool ShowMessage(DataRow row, int pi_Result)
        {
            switch (pi_Result)
            {
                case -3:
                    row.RejectChanges();
                    MessageBox.Show("Mã lồng nhau!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;

                case -1:
                    row.RejectChanges();
                    MessageBox.Show("Mã đã tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;
            }
            return false;
        }

        [Category("Editor properties"), DefaultValue(""), Description("The title text when adding.")]
        public string AddTitleText
        {
            get
            {
                return this.is_AddTitleText;
            }
            set
            {
                this.is_AddTitleText = value;
                this.Text = value;
            }
        }

        [DefaultValue(""), Category("Editor properties"), Description("The title text when editing.")]
        public string EditTitleText
        {
            get
            {
                return this.is_EditTitleText;
            }
            set
            {
                this.is_EditTitleText = value;
            }
        }

        [Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                return this.oCurrentRow;
            }
            set
            {
                this.oCurrentRow = value;
                if ((this.oCurrentRow != null) && (this.oCurrentRow.RowState == DataRowState.Unchanged))
                {
                    this.OnShow();
                }
                this.OnReset();
            }
        }

        [Browsable(false)]
        protected string LocalFieldName
        {
            get
            {
                return this.LocalFormLister.LocalFieldName;
            }
        }

        [Browsable(false)]
        public FormUserLister LocalFormLister
        {
            get
            {
                return this.oLocalFormLister;
            }
            set
            {
                this.oLocalFormLister = value;
            }
        }

        [Browsable(false)]
        private DataGridView LocalGridBrowser
        {
            get
            {
                return this.LocalFormLister.LocalGridBrowser;
            }
        }

        [Browsable(false)]
        public DataTable LocalTable
        {
            get
            {
                return this.LocalFormLister.LocalTable;
            }
        }

        [Browsable(false)]
        public bool NewChecked
        {
            get
            {
                return this.ib_NewChecked;
            }
            set
            {
                this.ib_NewChecked = value;
                this.Text = this.GetTitleMessage();
            }
        }

        [Browsable(false)]
        private string OrderFieldNames
        {
            get
            {
                return this.LocalFormLister.OrderFieldNames;
            }
        }
    }
}

